package vek.Objects;

import vek.XML.parsedObjects.ParsedOpeninghours;

/**
 * Deals with the opening hours of a POI.
 * The opening hours are defined as a start time and an end time for each day.
 * The start time is when the POI opens, the end time is when it closes.
 * -1 == closed
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public class Openinghours {
    
    private int[] weekStartTimes = new int[7]; //sunday == weekStartTimes[0], -1 == closed
    private int[] weekEndTimes = new int[7]; //sunday == weekEndTimes[0], -1 == closed
    
    /**
     * Creates an Openinghours object and assigns its varaibles using data from the corrosponding ParsedObject.
     * @param po The ParsedOpeninghours which contains all the data for this object
     */
    
    public Openinghours(ParsedOpeninghours po)
    {
        weekStartTimes = po.weekStartTimes;
        weekEndTimes = po.weekEndTimes;
    }
    
    /**
     * Checks if the POI is open at the specified time
     * @param currentDay the day which the poi is supposed to be open
     * @param currentHour the hour which the poi is supposed to be open
     * @param currentMinute the minute which the poi is supposed to be open
     * @return true if the POI is open at this time
     */
    
    public boolean isOpen(int currentDay, int currentHour, int currentMinute)
    {
        if( isDayClosed( weekStartTimes[currentDay] ) )
        {
            return false;
        }
        
        String startH = String.valueOf(weekStartTimes[currentDay]).substring(0, 2);
        String endH = String.valueOf(weekEndTimes[currentDay]).substring(0, 2);

        int startHour = Integer.parseInt(startH);
        int endHour = Integer.parseInt(endH);
        
        System.out.println("start: " + startHour);
        System.out.println("end: " + endHour);
        
        boolean lateOpen = false;
        if( endHour < startHour) // store is closing late, i.e. later than 24:00
        {
            lateOpen = true;
        }
        
        if( (currentHour >= startHour) && currentHour <= endHour && lateOpen == false)
        {
            if(currentHour == startHour)
            {
                if(isCurrentMinuteLargerThanStartMinute(currentDay, currentMinute))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if(currentHour == endHour)
            {
                if( isCurrentMinuteSmallerThanEndMinute(currentDay, currentMinute))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else if( (currentHour >= startHour) && currentHour >= endHour && lateOpen == true)
        {
            if(currentHour == startHour)
            {
                if(isCurrentMinuteLargerThanStartMinute(currentDay, currentMinute))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if(currentHour == endHour)
            {
                if( isCurrentMinuteSmallerThanEndMinute(currentDay, currentMinute))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
        
    }
    
    /**
     * Checks if the minute specified is larger then the start minute of the opening hours
     * @param day the day of the minute
     * @param minute the minute which is suppsed to be larger than the starting minute to return true
     * @return true if the specified minute is larger than the starting minute of the specified day.
     */
    
    private boolean isCurrentMinuteLargerThanStartMinute(int day, int minute)
    {
        String startM = String.valueOf(weekStartTimes[day]).substring(2, 4);
        int startMinute = Integer.parseInt(startM);
        
        if(minute > startMinute)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * Checks if the minute specified is smaller then the end minute of the opening hours
     * @param day the day of the minute
     * @param minute the minute which is suppsed to be smaller than the end minute to return true
     * @return true if the specified minute is smaller than the end minute of the specified day.
     */
    
    private boolean isCurrentMinuteSmallerThanEndMinute(int day, int minute)
    {
        String endM = String.valueOf(weekEndTimes[day]).substring(2, 4);
        int endMinute = Integer.parseInt(endM);
        
        if(minute < endMinute)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * Checks if the day is closed. closed == -1
     * @param day the day which is to be checked
     * @return true if day == -1
     */
    
    private boolean isDayClosed(int day)
    {
        if(day == -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
  
        
        
    

}
